const router = require('koa-router')()
const mysql = require('../../db')
const multer = require('koa-multer');
const path = require('path');
router.prefix('/after')

// 添加菜单数据   不要乱掉用
router.get('/addmenu', async (ctx, next) => {
  let sql = `insert into rolemenu(rm_r_id,rm_me_id)
  values(?,?)`
  for (let j = 1; j <= 16; j++) {
    for (let i = 1; i <= 21; i++) {
      await mysql(sql, [j,i]);
    }
  }
  ctx.body = {
    status: 1,
    message:"成功"
  }
})

let storage = multer.diskStorage({
  destination: path.resolve('public/images/carousel'),
  filename: (ctx, file, cb)=>{
    let suffix = file.originalname.split('.')[1];
      cb(null, Date.now()+"."+suffix);
  }
});
let fileFilter = (ctx, file ,cb)=>{
let suffix = file.originalname.split('.')[1];
  if (!suffix in ['jpg','png','jpeg','gif']){
      cb(null, false); 
  }else {
      cb(null, true); 
  }
}
let upload = multer({ storage: storage, fileFilter: fileFilter });

//管理员登录
router.post('/login', async (ctx, next) => {
  try {
    let { account, pwd } = ctx.request.body;
    let sql = 'select * from admin where a_account=? and a_password=? and a_status=1';
    let result = await mysql(sql, [account, pwd]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})
// 查询菜单
router.get('/getmenus/:roleid', async (ctx, next) => {
  try {
    let roleid = ctx.params.roleid;
    let sql = `select menu.me_id,menu.me_name,menu.me_state from rolemenu join menu on rolemenu.rm_me_id=menu.me_id where rolemenu.rm_r_id=? and me_pid=0`;
    let menu = await mysql(sql, [roleid]);
    sql = `select menu.me_id,menu.me_name,menu.me_url,menu.me_state,menu.me_pid from rolemenu join menu on rolemenu.rm_me_id=menu.me_id where rolemenu.rm_r_id=? and me_pid=?`;
    for (let i = 0; i < menu.length; i++) {
      menu[i].childmenu = await mysql(sql, [roleid,i+1]);
    }
    ctx.body = {
      status: 1,
      message:menu
    }
  } catch (error) {
    next(error);
  }
})

// 查询所有的管理员   
router.get('/admin', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = "select * from admin limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from admin',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) {
      next(error)
    } 
})
// 添加管理员
router.post('/admin', async (ctx, next) => {
    try {
      let { a_account, a_name, a_password, a_r_id } = ctx.request.body;
      a_r_id = parseInt(a_r_id);
      let sql = "insert into admin(a_account,a_name,a_password,a_r_id)values(?,?,?,?)";
      let pms = [ a_account,a_name,a_password,a_r_id ];
      let result = await mysql(sql, pms);
      result = result.affectedRows;
      if (result > 0) {
            ctx.body = {
            status: 1,
            message: result
         }
      }
     } catch (error) {
        next(error)
        } 
})
// 修改管理员的状态
// router.put('/admin', async (ctx, next) => {
//     try {
//       let { a_id, a_name, a_password, a_r_id } = ctx.request.body;
//       a_r_id = parseInt(a_r_id);
//       a_id = parseInt(a_id);
//       let sql = "update admin set a_name=?,a_password=?,a_r_id=? where a_id=?";
//       let pms = [ a_name,a_password,a_r_id,a_id];
//       let result = await mysql(sql, pms);
//       result = result.affectedRows;
//       if (result > 0) {
//         ctx.body = {
//         status: 1,
//         message: result
//         }
//       }
//     } catch (error) {
//       next(error);
//     }
// })
// 修改管理员职位
router.put('/admin', async (ctx, next) => {
  try {
    let { id, roleId } = ctx.request.body;
    let sql = `update admin set a_r_id=? where a_id=?`;
    let pms = [ roleId,id];
    let result = await mysql(sql, pms);
    result = result.affectedRows;
    if (result > 0) {
      ctx.body = {
      status: 1,
      message: result
      }
    }
  } catch (error) {
    next(error)
  }
})
// 禁用管理员
router.delete('/admin', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update admin set a_status=? where a_id=?`;
    let result = await mysql(sql, [status, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})
// 角色查询
router.get('/role', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = `select * from role limit ?,?`
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from role',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
  } catch (error) {
    next(error);
  }
})

router.get('/findRolemenu', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = `select admin.a_id,admin.a_name,admin.a_account,admin.a_status,role.r_nickname from admin , role where admin.a_r_id=role.r_id limit ?,?`
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from admin',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
  } catch (error) {
    next(error)
  }
})
// 主页能用的轮播图
router.get('/carouselNormal', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = `select carousel.ca_id,carousel.ca_img,(case when carousel.ca_state=1 then '正常' else '禁用' end) ca_status,product.p_name from carousel,product 
    where carousel.ca_p_id=product.p_id and carousel.ca_state=1 limit ?,?`;
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from carousel where ca_state=1',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
  } catch (error) {
    next(error)
  }
})
// 主页轮播图查询
router.get('/carousel', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = `select carousel.ca_id,carousel.ca_img,(case when carousel.ca_state=1 then '正常' else '禁用' end) ca_status,product.p_name from carousel,product 
    where carousel.ca_p_id=product.p_id limit ?,?`;
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from carousel',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
  } catch (error) {
    next(error)
  }
})
// 选择
router.get('/productsimg', async (ctx) => {
  try {
    let sql = 'select p_id,p_name from product';
    let result = await mysql(sql, []);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
// 主页轮播图添加
router.post('/carousel', upload.single('pic'), async (ctx, next) => {
    try {
      let { ca_p_id} = ctx.req.body
      ca_p_id = parseInt(ca_p_id)
      let file = ctx.req.file;
      ca_img = 'carousel/' + file.filename;
      let sql = `insert into carousel(ca_img,ca_state,ca_p_id) values(?,1,?)`;
      let result = await mysql(sql, [ca_img, ca_p_id]);
      if (result.affectedRows > 0) {
        ctx.body = `<h3>上传成功^_^</h3>
          <div>该页面将在<span id="time"></span>秒后退出！</div>
          <script>
            let time = 5;
            setInterval(()=>{
              document.getElementById("time").innerText = time;
              if(time<0){
                window.location.href = "http://localhost:3051/"
              }
              time--;
            },1000)
          </script>`;
      }
    } catch (error) {
      next(error)
    }
})
// 主页轮播图禁用
router.delete('/carousel', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update carousel set ca_state=? where ca_id=?`;
    let result = await mysql(sql, [ status,id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
}) 
// 分页查询所有用户
router.get('/user', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    let sql = "select * from users limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from users',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) {
      next(error)
    } 
})
// 禁用管理员
router.delete('/user', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update users set u_status=? where u_id=?`;
    let result = await mysql(sql, [status, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})

// 查询所有车型
router.get('/classify', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select * from classify limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from classify',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }  
    } catch (error) {
      next(error)
    } 
})
//添加车型状态
router.post('/classify', async (ctx, next) => {
  let { name } = ctx.request.body;
  try {
    let sql = 'insert into classify(c_nickname,c_status) values(?,1)'
    let result = await mysql(sql, [name]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
//修改车型
router.put('/classify', async (ctx, next) => {
  try {
    let { name, id } = ctx.request.body;
    id = parseInt(id);
    let sql = 'update classify set c_nickname=? where c_id=?';
    let result = await mysql(sql, [name, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})
// 删除车型
router.delete('/classify', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update classify set c_status=? where c_id=?`;
    let result = await mysql(sql, [status, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})

// 查询汽车能源
router.get('/energy', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select * from energy limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from energy',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) { 
      next(error)
    } 
})
// 添加新的能源
router.post('/energy', async (ctx, next) => {
    try {
      let { name } = ctx.request.body;
      let sql = "insert into energy (en_nickname,en_status)values(?,1)";
      let pms = [ name ];
      let result = await mysql(sql, pms);
      result = result.affectedRows;
      if (result > 0) {
            ctx.body = {
            status: 1,
            message: result
         }
      }
    } catch (error) {
      next(error);
    } 
})
// 修改汽车能源状态
router.put('/energy', async (ctx, next) => {
    try {
      let { name, id } = ctx.request.body;
      id = parseInt(id);
      let sql = "update energy set en_nickname=? where en_id=?";
      let pms = [ name,id];
      let result = await mysql(sql, pms);
      result = result.affectedRows;
      if (result > 0) {
            ctx.body = {
            status: 1,
            message: result
            }
      }
    } catch (error) {
           next(error)
        }
})
// 修改能源状态
router.delete('/energy', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update energy set en_status=? where en_id=?`;
    let result = await mysql(sql, [status, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})

 // 查询所有的厂商
router.get('/manufacturer', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select m_id,m_name,m_content,m_address,m_credit from manufacturer limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from manufacturer',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) {
      next(error);
    } 
})
// 添加新的厂商
router.post('/manufacturer', async (ctx, next) => {
    try {
      let { m_name,m_content,m_address } = ctx.request.body;
      let sql = "insert into manufacturer (m_name,m_content,m_address)values(?,?,?)";
      let pms = [ m_name,m_content,m_address];
      let result = await mysql(sql, pms);
      result = result.affectedRows;
      if (result > 0) {
            ctx.body = {
            status: 1,
            message: result
         }
      } 
    } catch (error) {
      next(error);
    } 
})
// 修改厂商信息   给厂商评星级
router.put('/manufacturer', async (ctx, next) => {
  try {
    let { m_id, m_name, m_content, m_address, m_credit } = ctx.request.body;
    m_id = parseInt(m_id);
    m_credit = parseInt(m_credit);
    let sql = `update manufacturer set m_name=?, m_content=?, m_address=?, m_credit=? where m_id=?`;
    let result = await mysql(sql, [ m_name, m_content, m_address, m_credit , m_id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})

// 与厂商终止合作
router.delete('/manufacturer:mId', async (ctx, next) => {
    try {
        let { mId } = ctx.params;
        let sql = "delete from manufacturer where m_id = ?";
        let result = mysql(sql, [mId]);
        ctx.body = {
            status: 1,
            message: result
        }
    } catch (error) {
      next(error);
    }
})

// 查询出所有商品
router.get('/products', async (ctx, next) => {
  try {
    let { indexPage, pageSize } = ctx.query;
    indexPage = parseInt(indexPage);
    pageSize = parseInt(pageSize);
    let startPage = parseInt((indexPage - 1) * pageSize);
    // console.log(pageSize,startPage);
    let sql = `select product.p_id,product.p_name,product.p_picture,p_number,p_importPrice,
    product.p_sellPrice,DATE_FORMAT(product.p_date,'%Y-%m-%d %H:%i:%S') p_date,
    energy.en_nickname,protect.pr_name,
    product.p_kw,product.p_nm,product.p_fdj,product.p_bs,product.p_ckg,product.p_cg,ifnull(product.p_km,'无') p_km,
    ifnull(product.p_s,'无') p_s,product.p_zp,product.p_sellNumber,product.p_productNumber,classify.c_nickname,
    product.p_particular,shop.s_name,manufacturer.m_name,
    (case when product.p_status=1 then '正常' else '禁用' end) status
    from product,energy,protect,classify,shop,manufacturer
    where energy.en_id=product.p_energy_id and 
    protect.pr_id=product.p_protect_id and classify.c_id=product.p_classify_id and shop.s_id=product.p_s_id
    and product.p_m_id=manufacturer.m_id
    order by product.p_id asc limit ?,?`;
    let result = await mysql(sql, [startPage, pageSize]);
    sql = 'select count(*) totalPage from product'
    let totalPage = await mysql(sql, []);
    totalPage = totalPage[0].totalPage
    ctx.body = {
      status: 1,
      message: result,
      totalPage
    }
  } catch (error) {
    next(error);
  }
})
// 注册商品
router.post('/products', async (ctx, next) => {
  try {
    let { p_name, p_number, p_importPrice, p_sellPrice, p_energy_id, p_protect_id, p_kw,
      p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, p_productNumber, p_classify_id, p_particular,
      p_s_id, p_m_id,data1,data2 } = ctx.request.body;
      p_number = parseInt(p_number);
      p_energy_id = parseInt(p_energy_id);
      p_protect_id = parseInt(p_protect_id);
      p_kw = parseInt(p_kw);
      p_nm = parseInt(p_nm);
      p_km = parseInt(p_km);
      p_classify_id = parseInt(p_classify_id);
      p_s_id = parseInt(p_s_id);
      p_m_id = parseInt(p_m_id);
      p_importPrice = parseFloat(p_importPrice);
      p_sellPrice = parseFloat(p_sellPrice);
      p_s = parseFloat(p_s);
    let sql = `insert into product(p_name, p_number, p_importPrice, p_sellPrice, p_date, p_energy_id, p_protect_id, p_kw,
      p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, 
       p_productNumber, p_classify_id,
       p_particular,P_s_id, p_m_id) 
      values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`;
    let result = await mysql(sql, [p_name, p_number, p_importPrice, p_sellPrice, data2, p_energy_id, p_protect_id, p_kw,
      p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, p_productNumber, p_classify_id, p_particular,
      p_s_id, p_m_id]);
    // console.log(p_name, p_prcture, p_number, p_importPrice, p_sellPrice, data2, p_energy_id, p_protect_id, p_kw,
    //     p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, p_discount_id, p_productNumber, p_classify_id, p_particular,
    //     p_s_id, p_m_id);
    ctx.body = {
      status: 1,
      message:result
    }
    // ctx.body = {
    //   a:"121"
    // }
  } catch (error) {
    next(error)
  }
})
// 商品图片：
// router.post('/imgcar', upload.single('pic'), async (ctx, next) => {
//   try {
//     let { img_url, img_p_id } = ctx.req.body
//     img_p_id = parseInt(img_p_id)
//     let sql = `insert into imgcar values(?,?)`;
//     let result = await mysql(sql, [img_url, img_p_id]);
//     let file = ctx.req.file;
//     ctx.body = {
//       status: 0,
//       message: {
//         imgInfo:result[0],
//         file:file,
//         fileName:file.filename
//       }
//     }
//   } catch (error) {
//     next(error)
//   }
// })
// 根据id修改商品
router.put('/products', async (ctx, next) => {
  try {
    let { p_id,p_name, p_prcture, p_number, p_importPrice, p_sellPrice, p_date, p_energy_id, p_protect_id, p_kw,
      p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, p_discount_id, p_productNumber, p_classify_id, p_particular,
      P_s_id, p_m_id } = ctx.request.body;
      p_id = parseInt(p_id);
      p_number = parseInt(p_number);
      p_energy_id = parseInt(p_energy_id);
      p_protect_id = parseInt(p_protect_id);
      p_kw = parseInt(p_kw);
      p_nm = parseInt(p_nm);
      p_km = parseInt(p_km);
      p_discount_id = parseInt(p_discount_id);
      p_classify_id = parseInt(p_classify_id);
      P_s_id = parseInt(P_s_id);
      p_m_id = parseInt(p_m_id);
      p_importPrice = parseFloat(p_importPrice);
      p_sellPrice = parseFloat(p_sellPrice);
      p_s = parseFloat(p_s);
    let sql = `update product set p_name=?, p_prcture=?, p_number=?, p_importPrice=?, p_sellPrice=?, p_date=?, p_energy_id=?, p_protect_id=?, p_kw=?,
    p_nm=?, p_fdj=?, p_bs=?, p_ckg=?, p_cg=?, p_km=?, p_s=?, p_zp=?, p_discount_id=?, p_productNumber=?, p_classify_id=?, p_particular=?,
    P_s_id=?, p_m_id=? where p_id=?`;
    let result = await mysql(sql, [p_name, p_prcture, p_number, p_importPrice, p_sellPrice, p_date, p_energy_id, p_protect_id, p_kw,
      p_nm, p_fdj, p_bs, p_ckg, p_cg, p_km, p_s, p_zp, p_discount_id, p_productNumber, p_classify_id, p_particular,
      P_s_id, p_m_id,p_id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
router.put('/productNumber', async (ctx, next) => {
  try {
    let { id, number } = ctx.request.body;
    number = parseInt(number);
    id = parseInt(id);
    console.log( id, number );
    let sql = `update product set p_number=p_number+? where p_id=?`;
    let result = await mysql(sql, [number, id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error);
  }
})
// 根据id删除商品
router.delete('/products', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update product set p_status=? where p_id=?`;
    let result = await mysql(sql, [status,id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})

 // 查询所有的店铺
 router.get('/shop', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select * from shop limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from shop',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) {
      next(error);
    } 
 })
 // 注册店铺
router.post('/shop', async (ctx, next) => {
  try {
    let { s_name, s_content,s_m_id } = ctx.request.body;
    s_m_id = parseInt(s_m_id);
    let sql = `insert into shop(s_name,s_class,s_fans,s_content,s_m_id) values(?,1,0,?,?)`;
    let result = await mysql(sql, [s_name, s_content,s_m_id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
 // 修改店铺
 router.put('/shop', async (ctx, next) => {
  try {
    let { s_name,s_class,s_fans, s_content,s_m_id,s_id } = ctx.request.body;
    s_id = parseInt(s_id);
    s_class = parseInt(s_class);
    s_fans = parseInt(s_fans);
    s_m_id = parseInt(s_m_id);
    let sql = `update shop set s_name=?,s_class=?,s_fans=?,s_content=?,s_m_id=? where s_id=?`;
    let result = await mysql(sql, [s_name,s_class,s_fans, s_content,s_m_id,s_id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
// 根据id删除店铺
router.delete('/shop', async (ctx, next) => {
  try {
    let { id, status } = ctx.query;
    id = parseInt(id);
    status = parseInt(status);
    let sql = `update shop set s_status=? where s_id=?`;
    let result = await mysql(sql, [status,id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
// 查询环保类型
router.get('/protect', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
      let sql = "select * from protect limit ?,?";
    let cars = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from protect',[])
    totalPage = totalPage[0].totalPage
      ctx.body = {
        status: 1,
        message: cars,
        totalPage
      }
    } catch (error) { 
      next(error)
    } 
})
// 查询全部订单
router.get('/orderAll', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    // 未接单
    let sql = `select ordertable.o_id,ordertable.o_serial,ordertable.o_date,
    ordertable.o_money,ordertable.o_address,ordertable.o_comment,
    (case when ordertable.o_status=1 then '未接单' else (case when o_status=0 then '已接单' else '已收货' end) end) o_status,
    users.u_nickname,product.p_name from ordertable , users , product 
    where users.u_id=ordertable.o_u_id and ordertable.o_p_id=product.p_id limit ?,?`;
    let order = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from ordertable',[])
    totalPage = totalPage[0].totalPage;
      ctx.body = {
        status: 1,
        message: order,
        totalPage
      }
  } catch (error) {
    next(error)
  }
})
// 订单查询
router.get('/order', async (ctx, next) => {
  try {
    let { currentsize, pagesize } = ctx.query;
    let indexsize = (currentsize - 1) * pagesize;
    pagesize = parseInt(pagesize);
    // 未接单
    let sql = `select ordertable.o_id,ordertable.o_serial,ordertable.o_date,
    ordertable.o_money,ordertable.o_address,ordertable.o_comment,
    (case when ordertable.o_status=1 then '未接单' else (case when o_status=0 then '已接单' else '已收货' end) end) o_status,
    users.u_nickname,product.p_name from ordertable , users , product 
    where users.u_id=ordertable.o_u_id and ordertable.o_p_id=product.p_id and 
    ordertable.o_status=1 limit ?,?`;
    let order = await mysql(sql, [indexsize, pagesize]);
    let totalPage = await mysql('select count(*) totalPage from ordertable where o_status=1',[])
    totalPage = totalPage[0].totalPage;
    // 已发货
    sql = `select ordertable.o_id,ordertable.o_serial,ordertable.o_date,
    ordertable.o_money,ordertable.o_address,ordertable.o_comment,
    (case when ordertable.o_status=1 then '未接单' else (case when o_status=0 then '已接单' else '已收货' end) end) o_status,
    users.u_nickname,product.p_name from ordertable , users , product 
    where users.u_id=ordertable.o_u_id and ordertable.o_p_id=product.p_id and 
    ordertable.o_status=0 limit ?,?`;
    let ordered = await mysql(sql, [indexsize, pagesize]);
    let totalPaged = await mysql('select count(*) totalPage from ordertable where o_status=0',[])
    totalPaged = totalPaged[0].totalPage
    // 已收货
    sql = `select ordertable.o_id,ordertable.o_serial,ordertable.o_date,
    ordertable.o_money,ordertable.o_address,ordertable.o_comment,
    (case when ordertable.o_status=1 then '未接单' else (case when o_status=0 then '已接单' else '已收货' end) end) o_status,
    users.u_nickname,product.p_name from ordertable , users , product 
    where users.u_id=ordertable.o_u_id and ordertable.o_p_id=product.p_id and 
    ordertable.o_status=3 limit ?,?`;
    let getordered = await mysql(sql, [indexsize, pagesize]);
    let gettotalPaged = await mysql('select count(*) totalPage from ordertable where o_status=3',[])
    gettotalPaged = gettotalPaged[0].totalPage
      ctx.body = {
        status: 1,
        message: {order,ordered,getordered},
        totalPage,
        totalPaged,
        gettotalPaged
      }
  } catch (error) {
    next(error)
  }
})
// 接单
router.put('/order', async (ctx, next) => {
  try {
    let { id } = ctx.request.body;
    let sql = `update ordertable set o_status=0 where o_id=?`;
    let result = await mysql(sql, [id]);
    ctx.body = {
      status: 1,
      message:result
    }
  } catch (error) {
    next(error)
  }
})
module.exports=router